package utility;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;
//http://my.oschina.net/u/2300159/blog/508089?p={{page}}
public class C3p0Utils {
	private static ComboPooledDataSource cpds=new ComboPooledDataSource(); 
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws SQLException{
		Connection connection=null;
		Statement statement=null;
		ResultSet resultSet=null;
		try {
			connection=C3p0Utils.getConnection();
			statement=connection.createStatement();
			
			String sql="insert into user(name,birthday, money) values ('name1', '1987-01-01', 400) ";

			int i = statement.executeUpdate(sql);
			System.out.println("i=" + i);
		} finally{
			DBCPUtils.close(resultSet, statement, connection);
		}
	}
	
	static public DataSource getInstance(){
		return cpds;
	}
	
	static public Connection getConnection() throws SQLException{
		return cpds.getConnection();
	}
	
	static public void close(ResultSet rs,Statement st,Connection con){
		try {
			if(rs!=null){
				rs.close();
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(st!=null){
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(con!=null){
						try {
							con.close();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
}
